import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shihui/common/component_index.dart';
import 'package:shihui/data/repository/worldexchange_repository.dart';
import 'package:shihui/ui/widgets/widgets.dart';
import 'package:shihui/utils/util_index.dart';
import 'package:webview_flutter/webview_flutter.dart';


class ProcessPage extends StatefulWidget {
  @override
  _ProcessPageState createState() {
    // TODO: implement createState
    return _ProcessPageState();
  }
}

class _ProcessPageState extends State<ProcessPage> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  WorldExchangeRepository worldExchangeRepository =
      new WorldExchangeRepository();

  bool loading = true;

  var isProcessInit = true;


  String _htmlData = "<html></html>";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  loadHtmlData() async {
    var htmlData = await DefaultAssetBundle.of(context)
        .loadString("assets/html/article.html");

    var article = await worldExchangeRepository.getProcessDetail();
    String locale = CustomLocalizations.of(context).locale.languageCode;
    String title;
    String content;
    switch (locale) {
      case "zh":
        title = article.title;
        content = article.content;
        break;
      case "en":
        title = article.etitle;
        content = article.econtent;
        break;
      case "th":
        title = article.ttitle;
        content = article.tcontent;
        break;
    }

    htmlData = htmlData.replaceAll("{{title}}", title);
    htmlData = htmlData.replaceAll("{{content}}", content);

    _controller.future.then((controller) {
      controller.loadData(htmlData);
      setState(() {
        loading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    if (isProcessInit) {
      LogUtil.e("ProcessPage init......");
      isProcessInit = false;
      loading = false;
      Observable.just(1).delay(new Duration(milliseconds: 500)).listen((_) {
        loadHtmlData();
      });
    }

    return Scaffold(
      appBar: AppBar(
        title: new Text(
          IntlUtil.getString(context, Ids.titleProcess),
          maxLines: 1,
          style: TextStyle(fontSize: ScreenUtil.getScaleSp(context, 16)),
          overflow: TextOverflow.ellipsis,
        ),
        centerTitle: true,
      ),
      body: Container(
        alignment: Alignment.center,
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            WebView(
              onWebViewCreated: (WebViewController webViewController) {
                _controller.complete(webViewController);
              },
              initialData: _htmlData,
              javascriptMode: JavascriptMode.unrestricted,
            ),
            Offstage(
              offstage: false,
              child: CircularProgressIndicator(),
            )
          ],
        ),
      ),
    );
  }
}
